package com.manager.plat.action;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.manager.plat.dao.IPartnerLowerDao;
import com.manager.plat.dao.IUserInfoDao;
import com.manager.plat.vo.PartnerLowerVo;
import com.manager.plat.vo.UserInfoVo;
import com.opensymphony.xwork2.ActionSupport;

public class PartnerLowerAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = -5528055551506251446L;

	private Logger logger = Logger.getLogger(PartnerLowerAction.class);
	
	private IPartnerLowerDao partnerLowerDao;		//渠道信息数据接口
	private IUserInfoDao userInfoDao;				//用户信息数据接口
	private List<PartnerLowerVo> partnerLowerVoList;//渠道信息数据列表
	private List<UserInfoVo> userInfoVoList;		//用户信息数据列表
	private PartnerLowerVo partnerLowerVo;			//渠道信息VO
	private String actionPath;						//操作成功返回路径
	
	/**获得渠道列表*/
	public String getPartnerLowerList(){
		partnerLowerVoList = partnerLowerDao.getPartnerLowerList();
		/**查询用户信息列表*/
		userInfoVoList = userInfoDao.getPartnerUserInfoList();
		return SUCCESS;
	}
	/**添加渠道信息*/
	public String addPartnerLower(){
		/**重置渠道信息*/
		partnerLowerVo = null;
		/**查询用户信息列表*/
		userInfoVoList = userInfoDao.getPartnerUserInfoList();
		
		return SUCCESS;
	}
	/**编辑渠道信息*/
	public String editPartnerLower(){
		if(partnerLowerVo.getId()  != 0){
			partnerLowerVo = partnerLowerDao.getPartnerLowerById(partnerLowerVo.getId());
		}
		/**查询用户信息列表*/
		userInfoVoList = userInfoDao.getPartnerUserInfoList();
		return SUCCESS;
	}
	
	/**更新渠道信息*/
	public String updatePartnerLower(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo userInfoVo = (UserInfoVo) request.getAttribute("user");
		try {
			partnerLowerVo.setName(URLDecoder.decode(partnerLowerVo.getName(), "UTF-8"));
		} catch (UnsupportedEncodingException e) {
			logger.error(String.format("更新渠道信息解码异常:%s", ExceptionUtils.getFullStackTrace(e)));
		}
		
		if(partnerLowerVo.getId() == 0){
			partnerLowerVo.setAdd_user(userInfoVo.getId());
			/**插入渠道信息*/
			int count = partnerLowerDao.insertPartnerLower(partnerLowerVo);
			
			if(count > 0){
				logger.info(String.format("[%s]添加[%s]渠道成功", userInfoVo.getLogin_username(),partnerLowerVo.getName()));
			}else{
				logger.info(String.format("[%s]添加[%s]渠道失败", userInfoVo.getLogin_username(),partnerLowerVo.getName()));
			}
		}else{
			partnerLowerVo.setUpdate_user(userInfoVo.getId());
			int count = partnerLowerDao.updatePartnerLower(partnerLowerVo);
			if(count > 0){
				logger.info(String.format("[%s]更新[%s]渠道成功", userInfoVo.getLogin_username(),partnerLowerVo.getName()));
			}else{
				logger.info(String.format("[%s]更新[%s]渠道失败", userInfoVo.getLogin_username(),partnerLowerVo.getName()));
			}
		}
		
		actionPath = "partnerLower/getPartnerLowerList.action";
		return SUCCESS;
	}
	
	/**删除渠道信息*/
	public String deletePartnerLower(){
		HttpServletRequest request = ServletActionContext.getRequest();
		UserInfoVo userInfoVo = (UserInfoVo) request.getAttribute("user");
		
		try {
			partnerLowerVo.setName(URLDecoder.decode(partnerLowerVo.getName(), "UTF-8"));
		} catch (UnsupportedEncodingException e) {
			logger.error(String.format("删除渠道信息解码异常:%s", ExceptionUtils.getFullStackTrace(e)));
		}
		
		int count = partnerLowerDao.deletePartnerLower(partnerLowerVo.getId(),userInfoVo.getId());
		if(count > 0){
			logger.info(String.format("[%s]删除[%s]渠道成功", userInfoVo.getLogin_username(),partnerLowerVo.getName()));
		}else{
			logger.info(String.format("[%s]删除[%s]渠道失败", userInfoVo.getLogin_username(),partnerLowerVo.getName()));
		}
		
		actionPath = "partnerLower/getPartnerLowerList.action";
		return SUCCESS;
	}
	public Logger getLogger() {
		return logger;
	}
	public void setLogger(Logger logger) {
		this.logger = logger;
	}
	public IPartnerLowerDao getPartnerLowerDao() {
		return partnerLowerDao;
	}
	public void setPartnerLowerDao(IPartnerLowerDao partnerLowerDao) {
		this.partnerLowerDao = partnerLowerDao;
	}
	public IUserInfoDao getUserInfoDao() {
		return userInfoDao;
	}
	public void setUserInfoDao(IUserInfoDao userInfoDao) {
		this.userInfoDao = userInfoDao;
	}
	public List<PartnerLowerVo> getPartnerLowerVoList() {
		return partnerLowerVoList;
	}
	public void setPartnerLowerVoList(List<PartnerLowerVo> partnerLowerVoList) {
		this.partnerLowerVoList = partnerLowerVoList;
	}
	public List<UserInfoVo> getUserInfoVoList() {
		return userInfoVoList;
	}
	public void setUserInfoVoList(List<UserInfoVo> userInfoVoList) {
		this.userInfoVoList = userInfoVoList;
	}
	public PartnerLowerVo getPartnerLowerVo() {
		return partnerLowerVo;
	}
	public void setPartnerLowerVo(PartnerLowerVo partnerLowerVo) {
		this.partnerLowerVo = partnerLowerVo;
	}
	public String getActionPath() {
		return actionPath;
	}
	public void setActionPath(String actionPath) {
		this.actionPath = actionPath;
	}
}